<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./cart.css">
    <style>
    </style>
</head>

<body>
    <div id="app">
        <div class="shoppingBar">
            <h2>购物清单</h2>
            <table>
                <tbody>
                    <tr>
                        <th><input type="checkbox" title="全选" class="selectAll"></th>
                        <th>商品名称</th>
                        <th>数量</th>
                        <th>单价（元）</th>
                        <th>金额（元）</th>
                        <th>操作</th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input select" value="1"></th>
                        <th class="content"><img src="img/1 (1).jpg" width="100px" height="100px">
                            <p>电视机 </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span class="spanNumber">
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            200
                        </th>
                        <th class="money">
                            200
                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input select" value="2"></th>
                        <th class="content"><img src="img/1 (2).jpg" width="100px" height="100px">
                            <p>洗衣机</p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span class="spanNumber">
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            150
                        </th>
                        <th class="money">
                            150
                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input select" value="3"></th>
                        <th class="content"><img src="img/1 (3).jpg" width="100px" height="100px">
                            <p>冰箱 </p>
                        </th>
                        <th><button type="button">-</button>
                            <span class="spanNumber">1</span>
                            <button type="button">+</button></th>
                        <th>
                            100
                        </th>
                        <th class="money">
                            100
                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                </tbody>
            </table>
            <div class="shoppingBar-footer">
                <div class="manage"><span class="delAll">
                        删除所选商品
                    </span> <span class="return">
                        继续购物
                    </span></div> <button id="go" type="button">
                    去结算
                </button>
                <div class="buy"><span>
                        0
                    </span>
                    件商品总计:
                    <span>
                        ￥0
                    </span></div>
            </div>
        </div>
    </div>
</body>
<script>
    /*
        1、加减按钮 点击 导致金额改变
        2、当勾选 某一个商品之后 需要计算 总金额
        3、当删除 某一个商品之后  需要计算总金额
    */
    // 通过事件委托绑定点击事件
    document.querySelector(".shoppingBar").addEventListener("click", function (event) {
        // 增加数量
        if (event.target.nodeName == 'BUTTON') {
            if (event.target.innerHTML.trim() == "+") {
                // 获取当前数量
                let number = event.target.previousElementSibling.innerHTML.trim();
                number++;
                // 修改数量innerHTML
                event.target.previousElementSibling.innerHTML = number;

                // 捕捉单价
                let price = event.target.parentElement.nextElementSibling.innerHTML.trim() - 0;
                // 修改数量后面小计的金额
                event.target.parentElement.nextElementSibling.nextElementSibling.innerHTML = price * number;
                // 修改商品总数
                //  document.querySelectorAll(".spanNumber").forEach(function(item,index){
                //     console.log(item.innerText)
                //      document.querySelectorAll('.buy>span')[0].innerHTML = item.innerText;
                //  })

                // }))
                //  console.log(wq)

                // let gs = 0;
                // gs += number;
                // document.querySelectorAll('.buy>span')[0].innerHTML = gs
                priceTotal()

            } else {
                // 获取当前数量
                // 减少数量
                let number = event.target.nextElementSibling.innerHTML.trim();
                if (number <= 1) {
                    alert("最少选择一件")
                    return;
                }
                number--;
                // 修改数量innerHTML
                event.target.nextElementSibling.innerHTML = number;
                // 捕捉单价，修改小计金额
                let price = event.target.parentElement.nextElementSibling.innerHTML.trim() - 0;
                event.target.parentElement.nextElementSibling.nextElementSibling.innerHTML = price * number;
                priceTotal()
            }
        } else if (event.target.nodeName == 'INPUT') {
            // 如果点的是全选框，让下面三个框都checked
            var that = this;
            if (event.target.className == "selectAll") {
                document.querySelectorAll(".input").forEach(item => {
                    item.checked = document.querySelector(".selectAll").checked;
                    // item.checked=that.checked;
                    console.log(this);
                    console.log(item);
                    console.log(111);
                })
                priceTotal()
            } else {
                console.log(234)
                document.querySelector('.selectAll').checked = (document.querySelectorAll('.input').length ==
                    document.querySelectorAll('.input:checked').length);
                priceTotal();
            }


        } else if (event.target.classList.contains('glyphicon-remove')) {
            document.querySelector('tbody').removeChild(event.target.parentElement.parentElement);
            priceTotal();
        }
        // document.querySelector(".selectAll").onclick=function(){
        //     document.querySelectorAll(".select").forEach(item=>{
        //         item.checked = this.checked;})
        // }

    })
    // 全选

    // 计算被勾选的商品的总金额是多少
    function priceTotal() {
        let total = 0;
        let per = 0;
        // 根据查找的被勾选的input框进行遍历
        document.querySelectorAll(".input:checked").forEach(item => {
            total += Number(item.parentElement.nextElementSibling.nextElementSibling.nextElementSibling
                .nextElementSibling.innerHTML.trim());
            per += Number(item.parentElement.nextElementSibling.nextElementSibling.firstElementChild
                .nextElementSibling.innerHTML
                .trim())
        })
        document.querySelectorAll('.buy>span')[0].innerHTML = per;
        document.querySelectorAll('.buy>span')[1].innerHTML = ' ￥' + total
    }
</script>

</html>